package demo1;


import java.util.LinkedList;
import java.util.Queue;

public class AchieveTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    // 获取树中节点的个数
    public int len = 0;

    int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        len++;
        size(root.left);
        size(root.right);
        return len;
    }

    // 获取叶子节点的个数
    public int size = 0;

    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        getLeafNodeCount(root.left);
        if (root.right == null) {
            size++;
        }
        getLeafNodeCount(root.right);
        return size;
    }

    int getLeafNodeCount2(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    // 子问题思路-求叶子结点个数
// 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    //    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) return 0;
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }

//    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode ret=find(root.left,val);
        if(ret!=null){
            return ret;
        }
       ret= find(root.right,val);
        return ret;
    }
    // 前序遍历
    void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.println(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    void inOrder(TreeNode root){
        if(root==null){
            return;
        }
       inOrder(root.left);
        System.out.println(root.val+" ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val+" ");
    }
//    //层序遍历
    void levelOrder(TreeNode root){
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return;
        }
        queue.offer(root);
        TreeNode cur;
        while(!queue.isEmpty()){
            cur=queue.poll();
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(root.right!=null){
                queue.offer(cur.right);
            }
            System.out.print(cur.val+" ");
        }
    }
//    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return true;
        }
        queue.offer(root);
        TreeNode cur;
        while(!queue.isEmpty()){
            cur=queue.poll();
            if(cur!=null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while(!queue.isEmpty()){
            if(queue.peek()==null){
                queue.poll();
            }else{
                return false;
            }
        }
        return true;
    }
}
